package com.hqyj.springBoot.modules.common.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author yun
 * @date 2021-5-8 17:04
 */
@Component
@ServerEndpoint("/api/webSocket")
public class WebSocketController {
    private final static Logger LOGGER = LoggerFactory.getLogger(WebSocketController.class);
    private static CopyOnWriteArraySet<Session> sessions = new CopyOnWriteArraySet<Session>();

    /**
     * - 创建连接
     * - 在此使用 @PathParam 接受 Path 参数
     */
    @OnOpen
    public void onOpen(Session session) {
        sessions.add(session);
        LOGGER.debug(String.format("新建连接，连接总数 %d。", sessions.size()));
    }

    // 关闭连接
    @OnClose
    public void onClose(Session session) {
        sessions.remove(session);
        LOGGER.debug(String.format("断开连接，连接总数 %d。", sessions.size()));
    }

    // 发生错误
    @OnError
    public void onError(Throwable throwable){
        LOGGER.debug("发生错误");
        throwable.printStackTrace();
    }

    // 接受客户端消息
    @OnMessage
    public void onMessage(String message) {
        LOGGER.debug(String.format("收到消息，%s，连接总数 %d。", message, sessions.size()));
        sendMessage("客户端，你好！消息已经收到，现在群发消息……");
    }

    // 向客户端群发信息
    public void sendMessage(String message) {
        LOGGER.debug(String.format("广播消息，%s，连接总数 %d。", message, sessions.size()));
        for (Session session : sessions) {
            try {
                session.getBasicRemote().sendText(message);
            } catch (IOException e) {
                e.printStackTrace();
                LOGGER.debug(e.getMessage());
            }
        }
    }
}
